/*filename: ppscommand.c*/

#include <arpa/inet.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "pps.h"
#include "ppsadapter.h"
#include "ppscommand.h"

#include "libcli/libcli.h"
#include "common/utils.h"
#include "config.h"

extern int optind;

struct option g_pps_long_opts[] = {
	{"direct",	required_argument,   	NULL,	'd'},
	{"action",	required_argument,	NULL,	'a'},
	{"offset",	required_argument,   	NULL,	'o'},
	{"len",	required_argument,   	NULL,	'l'},
	{"data",	required_argument,   	NULL,	12},
	{"comment",	required_argument,   	NULL,	'c'},
	{"help",	no_argument,   	NULL,	'h'},
	{0,		0,			0,	0},
};

int ConfigCli(struct cli_def *pCli)
{
	/*Set the prompt for cli*/
	cli_set_hostname(pCli, "PPS");

	/*Set the banner of cli*/
	cli_set_banner(pCli, "Welcome to PPS");

	/*Initialize authentication of cli*/
	cli_allow_user(pCli, "admin", "admin");

	/*Register commands to cli*/
	struct cli_command *pParentCmd = NULL,*pParentCmd1 = NULL;

	pParentCmd = cli_register_command(pCli, NULL, "host", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "manage host range");
	cli_register_command(pCli, pParentCmd, "show", ShowHostRange, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show IP range cared.");

	pParentCmd = cli_register_command(pCli, NULL, "network", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "show PPS network card");
	cli_register_command(pCli, pParentCmd, "show", ShowDeviceDMA, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "show PPS network card");

	pParentCmd = cli_register_command(pCli, NULL, "status", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "show PPS Work Status");
	cli_register_command(pCli, pParentCmd, "show", ShowStatus, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "show PPS Work Status");


	pParentCmd = cli_register_command(pCli, NULL, "debug", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "debug");
	pParentCmd1 = cli_register_command(pCli, pParentCmd, "level", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "debug leve;");
	cli_register_command(pCli, pParentCmd1, "show", DebugShowLevel, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "show debug level");
	cli_register_command(pCli, pParentCmd1, "set", DebugSetLevel, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "set debug level");


	pParentCmd1 = cli_register_command(pCli, pParentCmd, "globe", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "pps Globe");
	cli_register_command(pCli, pParentCmd1, "debug", GlobeSetDebug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "debug switch on/off");

	return 0;
}

/*======================================================================================*/
/*				About network card Configuration					*/
/*======================================================================================*/

int ShowDeviceDMA(struct cli_def *cli, char *szCmd, char *argv[], int argc)
{
	char cardinfo[1000];
	cardinfo[0]=0;  

	pps_get_DMA(cardinfo);

	if (0 == strlen(cardinfo)){
		cli_print(cli, "no information .");
		return CLI_OK;
	}

	cli_print(cli, "%s", cardinfo);

	return CLI_OK;

}

/*======================================================*/
/*              About Host configuration                 */
/*======================================================*/
int ShowHostRange(struct cli_def *cli, char *szCmd, char *argv[], int argc)
{
	struct ipscope tbl[PPS_MAX_IPSCOPE_NUM];
	char start[20];
	char end[20];

	int n = 0;
	int i = 0;

	cli_print(cli, "id\t start\t\t end\t\t\t flag");

	n = pps_export_ipscope_tbl(tbl, PPS_MAX_IPSCOPE_NUM);

	for(i=0; i<n; i++){
		ipaddr2str(tbl[i].m_start, start);
		ipaddr2str(tbl[i].m_end, end);
		cli_print(cli, "%d\t %s\t %s\t %d", tbl[i].m_id, start, end,tbl[i].m_flag);
	}

	return CLI_OK;
}

/*======================================================================================*/
/*                              Show PPS Status		                                 */
/*======================================================================================*/
int ShowStatus(struct cli_def *cli, char *szCmd, char *argv[], int argc)
{
	char statusinfo[2000];
	statusinfo[0]=0;  

	pps_get_status(statusinfo);

	if (0 == strnlen(statusinfo, 2000)){
		cli_print(cli, "no information .");
		return CLI_OK;
	}

	cli_print(cli, "%s", statusinfo);

	return CLI_OK;
}

int DebugShowLevel(struct cli_def *cli, char *szCmd, char *argv[], int argc)
{
	cli_print(cli, "Debug level is %d\n",pps_get_debug_level());

	return CLI_OK;
}

int DebugSetLevel(struct cli_def *cli, char *szCmd, char *argv[], int argc)
{ 
	int val=0;
	if (argc < 1){
		cli_print(cli, "%s: too few parameters.", szCmd);
		return CLI_OK;
	}
	val=atoi(argv[0]);
	if((val<0)||(val>5)){
		cli_print(cli, "level:0-5.");
		return CLI_OK;
	}
	pps_set_debug_level(val);
	cli_print(cli, "debug level set to %d .",val);

	return CLI_OK;
}

int GlobeSetDebug(struct cli_def *cli, char *szCmd, char *argv[], int argc)
{
        if (argc < 1){
                cli_print(cli, "%s: too few parameters.", szCmd);
                return CLI_OK;
        }
        if(strcmp(argv[0],"on")==0){
                pps_set_globe_debug(1);
                cli_print(cli, "CAS debug is on.");
        }else
        {
                if(strcmp(argv[0],"off")==0){
                        pps_set_globe_debug(0);
                        cli_print(cli, "CAS debug is off .");
                }else
                {
                        cli_print(cli, "usage: on|off .");
                }
        }
        return CLI_OK;
}

